/*
 *  atBudget Personal Finance Management application
 *
 *  Copyright 2011, Ilia Frenkel <frenkel.ilia@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
	New transaction dialogue.
 */
Ext.define('ATB.view.transaction.Create', {
	extend: 'Ext.window.Window',
	alias : 'widget.transactioncreate',

	title : 'New Transaction',
	layout: 'fit',
	autoShow: true,
	resizable: false,
	modal: true,
	width: 370,
	
	initComponent: function() {
		var me = this;
		
		this.items = [
			{
				xtype: 'form',
				bodyPadding: 10,
				border: false,
				defaults: {
					margin: '10 0'
				},
				items: [{
					xtype: 'fieldcontainer',
					anchor: '100%',
					fieldLabel: 'Amount/Rate',
					labelWidth: 90,
					layout: 'hbox',
					defaults: {
						margin: '0 1',
						labelSeparator: '',
						labelStyle: 'font-size: 80%'
					},
					fieldDefaults: {
						msgTarger: 'under',
						labelAlign: 'top'
					},
					items: [{
						xtype: 'button',
						icon: 'res/img/icon-minus16x16.png',
						text: '',
						anchor: 'b',
						tooltip: 'Debit',
						enableToggle: true,
						pressed: true,
						name: 'isdebit',
						listeners: {
							'toggle': function (btn, isPressed, opts) {
								if (isPressed) {
									this.setIcon('res/img/icon-minus16x16.png');
									this.setTooltip('Debit');
								} else {
									this.setIcon('res/img/icon-plus16x16.png');
									this.setTooltip('Credit');
								}
							}
						}
					}, {
						xtype: 'numberfield',
						hideTrigger: true,
						tabIndex: 120,
						flex: 3,
						fieldLabel: '',
						labelWidth: 90,
						name: 'amount',
						allowDecimals: true,
						allowBlank: false,
						minValue: 0,
						value: '0.00',
						emptyText: 'Amount'
					}, {
						xtype: 'numberfield',
						hideTrigger: true,
						tabIndex: 125,
						flex: 1,
						fieldLabel: '',
						labelWidth: 90,
						name: 'rate',
						allowDecimals: true,
						decimalPrecision: 4,
						allowBlank: false,
						minValue: 0,
						value: '1.00',
						emptyText: 'Rate'
					}]
				}, {
					xtype: 'fieldcontainer',
					anchor: '100%',
					fieldLabel: 'Category',
					labelWidth: 90,
					layout: 'hbox',
					defaults: {
						margin: '0 1',
						labelSeparator: '',
						labelStyle: 'font-size: 80%'
					},
					fieldDefaults: {
						msgTarger: 'under',
						labelAlign: 'top'
					},
					items: [{
						xtype: 'categoryselect',
						tabIndex: 130,
						flex: 1,
						name: 'category',
						fieldLabel: '',
						allowBlank: false,
						forceSelection: true,
						typeAhead: true,
						store: 'Categories',
						queryMode: 'local',
						displayField: 'name',
						valueField: 'id',
						emptyText: 'Select category'
					}, {
						xtype: 'button',
						icon: 'res/img/icon-add16x16.gif',
						text: '',
						minWidth: 16,
						tooltip: 'Create new category',
						action: 'newcategory'
					}]
				}, {
					xtype: 'fieldcontainer',
					anchor: '100%',
					fieldLabel: 'Payee',
					labelWidth: 90,
					layout: 'hbox',
					defaults: {
						margin: '0 1',
						labelSeparator: '',
						labelStyle: 'font-size: 80%'
					},
					fieldDefaults: {
						msgTarger: 'under',
						labelAlign: 'top'
					},
					items: [{
						xtype: 'combo',
						tabIndex: 140,
						flex: 1,
						name : 'payee',
						fieldLabel: '',
						allowBlank: false,
						forceSelection: true,
						store: 'Payees',
						queryMode: 'local',
						displayField: 'name',
						valueField: 'id',
						emptyText: 'Select payee'
					}, {
						xtype: 'button',
						icon: 'res/img/icon-add16x16.gif',
						text: '',
						minWidth: 16,
						tooltip: 'Create new payee',
						action: 'newpayee'
					}]
				}, {
					xtype: 'fieldcontainer',
					anchor: '100%',
					fieldLabel: 'Account',
					labelWidth: 90,
					layout: 'hbox',
					defaults: {
						margin: '0 1',
						labelSeparator: '',
						labelStyle: 'font-size: 80%'
					},
					fieldDefaults: {
						msgTarger: 'under',
						labelAlign: 'top'
					},
					items: [{
						xtype: 'combo',
						tabIndex: 170,
						flex: 1,
						name : 'account',
						fieldLabel: '',
						allowBlank: true,
						editable: false,
						forceSelection: true,
						store: 'Accounts',
						queryMode: 'local',
						displayField: 'name',
						valueField: 'id',
						emptyText: 'Select account',
						enableKeyEvents: true,
						listeners: {
							'keyup': function(field, evnt, opts) {
								if(evnt.keyCode == 46) {
									field.reset();
								}
							}
						}
					}, {
						xtype: 'button',
						icon: 'res/img/icon-add16x16.gif',
						text: '',
						minWidth: 16,
						tooltip: 'Create new account',
						action: 'newaccount'
					}]
				}]
			}
		];

		this.buttons = [
			{
				text: 'Save',
				tabIndex: 190,
				action: 'save'
			},
			{
				text: 'Cancel',
				tabIndex: 200,
				scope: this,
				handler: this.close
			}
		];

		/*
		 * Focus on "amount" field on open.
		 */
		this.on('show', function(wnd) {
			wnd.down('numberfield[name=amount]').focus(true, 200);
		});
		
		this.callParent(arguments);
	}//initComponent
});
